using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace StringFormatterAddin
{
    public class Configuration
    {
        private static readonly Configuration Instance = new Configuration();
        private Configuration()
        {
            InitializeRegistryStore();
        }

        private StringDictionary settingsCache;
        private Microsoft.Win32.RegistryKey regKey;
        private void InitializeRegistryStore()
        {
            string key = "Software\\StringFormatter";
            settingsCache = new StringDictionary();
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(key, true);
            if (regKey == null)
                regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(key);
        }

        private string GetSetting(string key, string defaultValue)
        {
            string value;
            if (!settingsCache.ContainsKey(key))
            {
                object obj = regKey.GetValue(key);
                if (obj == null)
                {
                    regKey.SetValue(key, defaultValue);
                    value = defaultValue;
                }
                else
                {
                    value = obj.ToString();
                }
                settingsCache.Add(key, value);
            }
            else
                value = settingsCache[key];
            return value;
        }

        private void SetSetting(string key, string value)
        {
            settingsCache[key] = value;
            regKey.SetValue(key, value);
        }

        public static bool EnableFormat
        {
            get
            {
                return bool.Parse(Instance.GetSetting("EnableFormat", "true"));
            }
            set
            {
                Instance.SetSetting("EnableFormat", value.ToString());
            }
        }

        public static bool EnableConcat
        {
            get
            {
                return bool.Parse(Instance.GetSetting("EnableConcat", "true"));
            }
            set
            {
                Instance.SetSetting("EnableConcat", value.ToString());
            }
        }
    }
}
